<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Settle_desc extends Model
{
	//
	protected $table = 'settle_desc';
	protected $fillable = [
		'store_station_id', 'strff_name', 'should_hour', 'actual_hour', 'settle_hour',
		'config_hour', 'transfer_hour', 'hour_salary', 'stop_hour', 'nonstandard_hour'
		, 'status', 'standard_money', 'reward', 'actual_money', 'settle_id'
	];
	protected $hidden = [];


	public function lists($settle_id)
	{
		return $this->where(['settle_id' => $settle_id])->get();
	}

	public function add_desc($id, $store_id)
	{
		$store_station = new Store_station();
		$data = $store_station->allstaff($store_id);

		$newArr = [];
		foreach ($data as $kk => $vv) {
//			$newArr[$kk]['store_station_id']=$vv['id'];
//			$newArr[$kk]['strff_name']=$vv['id'];
//			$newArr[$kk]['strff_name']=$vv['id'];
//			$newArr[$kk]['strff_name']=$vv['id'];
//			$newArr[$kk]['strff_name']=$vv['id'];
//			$newArr[$kk]['strff_name']=$vv['id'];
//			$newArr[$kk]['strff_name']=$vv['id'];
		}

		return $data;
		$this->create($data);
	}


	public function reward_lists($store_id, $store_station_id)
	{
		return $this->where(['store_id' => $store_id, 'store_station_id' => $store_station_id])->with('StaffInfo')->get();
	}


	public function StaffInfo()
	{
		return $this->belongsTo('App\Model\Staff', 'staff_id');
	}


	public function Settlement()
	{
		return $this->belongsTo('App\Model\Settlement', 'settle_id');
	}

	public function station_staff($where)
	{
		$data = $this->where(function ($query) use ($where) {
			$query->where(['store_id' => $where['store_id']]);
			$query->where(['start_time' => $where['dataTime']])->orwhere('end_time', '=', $where['dataTime']);
			$query->whereNotNull('standard_money');
		})->get();
		$newArr = [];
		foreach ($data as $k => $v) {
			$newArr[$v['store_station_id']]['data'][] = $v;
		}
		foreach ($newArr as $kk => $vv) {
			$newArr[$kk]['station_name'] = $vv['data'][0]['station_name'];
			$newArr[$kk]['settle_num'] = count($vv['data']);
			$newArr[$kk]['store_station_id'] = $kk;
			$newArr[$kk]['reward_sum'] = 0;
			$newArr[$kk]['settle_sum'] = 0;
			foreach ($vv['data'] as $i => $j) {
				$newArr[$kk]['people_num'][] = $j['staff_id'];
				$newArr[$kk]['reward_sum'] = $newArr[$kk]['reward_sum'] + $j['reward'];
				$newArr[$kk]['settle_sum'] = $newArr[$kk]['settle_sum'] + $j['standard_money'];
			}
			$newArr[$kk]['people_num'] = count(array_unique($newArr[$kk]['people_num']));
		}
		return $newArr;
	}


	public function station_list($where)
	{
		$data = $this->where(function ($query) use ($where) {
			$query->where(['store_station_id' => $where['store_station_id']]);
			$query->where(function ($query) use ($where) {
				$query->where('start_time', '=', $where['dateTime'])->orwhere('end_time', '=', $where['dateTime']);
			});
		})->with('StaffInfo')->get();

		$result=[];
		foreach ($data as $k => $v) {
			$result[$v['staff_id']]['data'][] = $v;
		}


		foreach ($result as $kk => $vv) {
			foreach ($vv['data'] as $key => $value) {
				$vv['data'][$key]['all_sign'] = $this->all_sign($value['settle_id'], $value['store_station_id'], $value['staff_id']);
				$vv['data'][$key]['sign'] = $this->sign($value['settle_id'], $value['store_station_id'], $value['staff_id']);
			}
		}
		foreach ($result as $kk => $vv) {
			$result[$kk]['all_sign'] = 0;
			$result[$kk]['sign'] = 0;
			$result[$kk]['sum_reward']=0;
			$result[$kk]['standard_money']=0;
			foreach ($vv['data'] as $k => $v) {
				$result[$kk]['avator'] = $v['StaffInfo']['avator'];
				$result[$kk]['staff_name'] = $v['staff_name'];
				$result[$kk]['staff_id'] = $v['staff_id'];
				$result[$kk]['all_sign'] = $result[$kk]['all_sign'] + $v['all_sign'];
				$result[$kk]['realdy'] = $result[$kk]['sign'] + $v['sign'];
				$result[$kk]['sum_reward']=$result[$kk]['sum_reward']+$v['reward'];
				$result[$kk]['standard_money']=$result[$kk]['standard_money']+$v['standard_money'];
			}
			$result[$kk]['sum_sign']=$result[$kk]['all_sign'].'/'.$result[$kk]['realdy'];
			$result[$kk]['leave']=$this->leave($result[$kk]['staff_id'],$where['dateTime']);
		}
		return $result;

	}

//	签到情况(所有)
	public function all_sign($settle_id, $station_id, $staff_id)
	{
		$query = ['settle_id' => $settle_id, 'station_id' => $station_id, 'staff_id' => $staff_id];
		$data = Sch_desc::where($query)
			->count();
		return $data;
	}

	//签到情况（已签)
	public function sign($settle_id, $station_id, $staff_id)
	{
		$query = ['settle_id' => $settle_id, 'station_id' => $station_id, 'staff_id' => $staff_id];
		$data = Sch_desc::where($query)->where(function ($query) {
			$query->whereNull('sign_start');
			$query->whereNull('sign_end');
		})->count();

		return $data;
	}

//	计算某一结算时间段中止时长
	public function leave($staff_id,$dataTime){
	return Leave::where(['staff_id'=>$staff_id])->where(function ($query) use($dataTime){
			$query->where('start_time','<',$dataTime)->where('end_time','>',$dataTime);
		})->sum('time');
	}
}
